package com.qk.management.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.util.PageResult;
import com.qk.common.enums.ParamEnum;
import com.qk.common.exception.CommonException;
import com.qk.dto.acticity.ActivityDto;
import com.qk.entity.Activity;
import com.qk.management.mapper.ActivityMapper;
import com.qk.management.service.ActivityService;
import com.qk.vo.ActivityVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
@Slf4j
@Service
public class ActivityServiceImpl implements ActivityService {
    @Autowired
    private ActivityMapper activityMapper;
    @Override
    public PageResult<ActivityVO> getActivity(ActivityDto activityDto) {
        PageHelper.startPage(activityDto.getPage(),activityDto.getPageSize());
        List<ActivityVO> list = activityMapper.getActivity(activityDto);
        Page<ActivityVO> page = (Page<ActivityVO>) list;
        return PageResult.<ActivityVO>builder()
                .total((int)page.getTotal())
                .rows(list)
                .build();
    }

    @Override
    public void deleteActivity(Integer activityid) {
        activityMapper.deleteActivity(activityid);
    }

    @Override
    public void addActivity(Activity activity) {
        boolean b = BeanUtil.hasNullField(activity,  "description","createTime", "updateTime");
        if(b){
            CommonException.throwCommonException(ParamEnum.PARAM_ERROR);
        }
        activity.setCreateTime(LocalDateTime.now());
        activity.setUpdateTime(LocalDateTime.now());
        activityMapper.insert(activity);
    }

    @Override
    public Activity selectById(Integer activityid) {
        return activityMapper.selectById(activityid);
    }

    @Override
    public void updateActivity(Activity activity) {
        activity.setUpdateTime(LocalDateTime.now());
        activityMapper.updateActivity(activity);
    }

    @Override
    public List<ActivityVO> selectByType(Integer type) {
        return activityMapper.selectByType(type);
    }
}
